class browser_logic :
    def __init__ ( self , mediator ) :
        self . _mediator = mediator
        self . _action_text = None
        self . _action_title_name_text = None
        self . _active_entry = None
        self . _all_knowledge_bases = None
        self . _already_exists_text = None
        self . _awake = False
        self . _edited_entry = None
        self . _entries = None
        self . _entries_by_row = None
        self . _entry_in_focus = None
        self . _only_unique_names = False
        self . _remove_action_part_when_no_entries = False
        self . _title_text = None
        self . _title_text_when_no_entries = None
        self . _entry_text_in_browser = None
    def entry_text_in_browser_reply ( self , text ) :
        self . _entry_text_in_browser = text
    def show_browser ( self ) :
        self . _awake = True
        self . _obtain_all_knowledge_bases ( )
        self . _add_title_label ( )
        self . _build_table ( )
        self . _build_entries ( )
        self . _select_active_entry_row ( )
        self . _move_edited_entry_marker ( )
        self . _add_return_button ( )
        if not self . _remove_action_part_when_no_entries or self . _is_any_entries ( ) :
            self . _copy_active_entry_name_to_text_entry ( )
            self . _add_action_button ( )
    def hide_browser ( self ) :
        self . _awake = False
    def configure_title_text_for_browser ( self , title_text ) :
        self . _title_text = title_text
    def configure_action_text_for_browser ( self , action_text ) :
        self . _action_text = action_text
    def configure_action_title_name_text_for_browser ( self , action_title_name_text ) :
        self . _action_title_name_text = action_title_name_text
    def configure_only_unique_names_mode_for_browser ( self , already_exists_text ) :
        self . _already_exists_text = already_exists_text
        self . _only_unique_names = True
    def configure_unique_or_existing_names_mode_for_browser ( self ) :
        self . _only_unique_names = False
    def configure_title_text_when_no_entries_for_browser ( self , title_text ) :
        self . _title_text_when_no_entries = title_text
    def configure_remove_action_part_when_no_entries_for_browser ( self ) :
        self . _remove_action_part_when_no_entries = True
    def configure_keep_action_part_when_no_entries_for_browser ( self ) :
        self . _remove_action_part_when_no_entries = False
    def receive_knowledge_base ( self , name , creation_time , modification_time , size ) :
        self . _all_knowledge_bases [ name ] = \
            { 'creation_time' : creation_time
            , 'modification_time' : modification_time
            , 'size' : size
            }
    def rename_base_in_browser ( self ) :
        if self . _edited_entry != None :
            self . _mediator . entry_text_in_browser_request ( )
            self . _mediator . rename_knowledge_base \
                ( self . _edited_entry . name 
                , self . _entry_text_in_browser
                )
        self . _refresh_browser ( )
    def copy_base_in_browser ( self ) :
        if self . _edited_entry != None :
            self . _mediator . entry_text_in_browser_request ( )
            self . _mediator . copy_knowledge_base \
                ( self . _edited_entry . name 
                , self . _entry_text_in_browser 
                )
        self . _refresh_browser ( )
    def on_browser_action_gui_button ( self ) :
        self . _proceed_to_browser_action ( )
    def on_browser_entry_unfocus ( self ) :
        self . _entry_in_focus = False
    def on_browser_entry_focus ( self ) :
        self . _entry_in_focus = True
    def on_key_down ( self ) :
        if self . _awake and self . _active_entry != None :
            self . _select_entry ( self . _active_entry . next )
    def on_key_up ( self ) :
        if self . _awake and self . _active_entry != None :
            self . _select_entry ( self . _active_entry . prev )
    def on_key_return ( self ) :
        if self . _awake :
            if not self . _entry_in_focus :
                if not self . _remove_action_part_when_no_entries or self . _is_any_entries ( ) :
                    self . _copy_active_entry_name_to_text_entry ( )
                self . _move_edited_entry_marker ( )
            else :
                self . _proceed_to_browser_action ( )
    def on_key_escape ( self ) :
        if self . _awake :
            self . _mediator . on_browser_return ( )
    def on_table_row_click ( self , row ) :
        if self . _awake :
            if row in self . _entries_by_row :
                self . _select_entry ( self . _entries_by_row [ row ] )
                self . _copy_active_entry_name_to_text_entry ( )
                self . _move_edited_entry_marker ( )
    def _refresh_browser ( self ) :
        self . _mediator . hide_browser ( )
        self . _mediator . show_browser ( )
    def _obtain_all_knowledge_bases ( self ) :
        self . _all_knowledge_bases = { }
        self . _mediator . fetch_all_knowledge_bases ( )
    def _build_table ( self ) :
        self . _mediator . add_table_to_browser ( )
        if self . _is_any_entries ( ) :
            self . _add_existing_menu_title ( )
            self . _add_existing_menu_entries ( )
            self . _add_empty_row ( )
        if not self . _remove_action_part_when_no_entries or self . _is_any_entries ( ) :
            self . _add_action_menu_title ( )
            self . _add_action_menu_entry ( )
    def _build_entries ( self ) :
        self . _create_existing_entries_list ( )
        self . _link_existing_entries ( )
        self . _determine_active_entry ( )
    def _select_entry ( self , entry ) :
        self . _unfocus_entry ( )
        self . _unselect_active_entry_row ( )
        self . _active_entry = entry
        self . _select_active_entry_row ( )
    def _select_active_entry_row ( self ) :
        if self . _active_entry != None :
            self . _mediator . select_table_row ( self . _active_entry . row )
    def _unselect_active_entry_row ( self ) :
        self . _mediator . unselect_table_row ( self . _active_entry . row )
    def _add_title_label ( self ) :
        if self . _is_any_entries ( ) :
            self . _mediator . add_title_label_to_browser ( self . _title_text )
        else :
            self . _mediator . add_title_label_to_browser ( self . _title_text_when_no_entries )
    def _add_existing_menu_title ( self ) :
        self . _mediator . add_title_row_to_browser \
            ( number = self . _mediator . localization_browser_number ( )
            , name = self . _mediator . localization_browser_existing_name ( )
            , creation_time = self . _mediator . localization_browser_creation_time ( )
            , modification_time = self . _mediator . localization_browser_modification_time ( )
            , size = self . _mediator . localization_browser_size ( )
            )
    def _add_existing_menu_entries ( self ) :
        entry_number = 0
        for name , entry in sorted ( self . _all_knowledge_bases . items ( ) ) :
            entry_number += 1
            self . _mediator . add_existing_row_to_browser \
                ( number = '%02i' % entry_number
                , name = name
                , creation_time = entry [ 'creation_time' ]
                , modification_time = entry [ 'modification_time' ]
                , size = entry [ 'size' ]
                )
    def _add_empty_row ( self ) :
        self . _mediator . add_empty_row_to_browser ( )
    def _add_action_menu_title ( self ) :
        self . _mediator . add_title_row_to_browser \
            ( number = self . _mediator . localization_browser_number ( )
            , name = self . _action_title_name_text
            , creation_time = self . _mediator . localization_browser_creation_time ( )
            , modification_time = self . _mediator . localization_browser_modification_time ( )
            , size = self . _mediator . localization_browser_size ( )
            )
    def _add_action_menu_entry ( self ) :
        self . _mediator . add_action_row_to_browser ( )
    def _add_return_button ( self ) :
        self . _mediator . add_return_button_to_browser ( )
    def _add_action_button ( self ) :
        self . _mediator . add_action_button_to_browser ( self . _action_text )
    def _create_existing_entries_list ( self ) :
        class menu_entry :
            def __init__ ( self , name , row ) :
                self . prev = None
                self . next = None
                self . name = name
                self . row = row
        self . _entries = [ ]
        self . _entries_by_row = { }
        entry_number = 0
        row = 0
        for name in sorted ( self . _all_knowledge_bases . keys ( ) ) :
            entry_number += 1
            row += 1
            entry = menu_entry ( name , entry_number )
            self . _entries += [ entry ]
            self . _entries_by_row [ row ] = entry
    def _link_existing_entries ( self ) :
        prev_entry = None
        for entry in self . _entries :
            if prev_entry != None :
                entry . prev = prev_entry
                prev_entry . next = entry
            prev_entry = entry
        if prev_entry != None :
            self . _entries [ 0 ] . prev = self . _entries [ -1 ]
            self . _entries [ -1 ] . next = self . _entries [ 0 ]
    def _determine_active_entry ( self ) :
        self . _active_entry = None if not self . _entries else self . _entries [ 0 ]
        self . _edited_entry = None
        for entry in self . _entries :
            current_time = self . _all_knowledge_bases [ entry . name ] [ 'modification_time' ]
            active_time = self . _all_knowledge_bases [ self . _active_entry . name ] [ 'modification_time' ]
            if current_time > active_time :
                self . _active_entry = entry
    def _copy_active_entry_name_to_text_entry ( self ) :
        self . _focus_entry ( )
        if self . _active_entry != None :
            self . _mediator . set_entry_text_in_browser ( self . _active_entry . name )
    def _focus_entry ( self ) :
        self . _entry_in_focus = True
        self . _mediator . focus_entry_in_browser ( )
    def _unfocus_entry ( self ) :
        self . _entry_in_focus = False
        self . _mediator . unfocus_entry_in_browser ( )
    def _bold_edited_entry ( self ) :
        if self . _edited_entry != None :
            self . _mediator . bold_table_row ( self . _edited_entry . row )
    def _unbold_edited_entry ( self ) :
        if self . _edited_entry != None :
            self . _mediator . unbold_table_row ( self . _edited_entry . row )
    def _move_edited_entry_marker ( self ) :
        if self . _active_entry != None :
            self . _unbold_edited_entry ( )
            self . _edited_entry = self . _active_entry
            self . _bold_edited_entry ( )
    def _filter_entry_text ( self ) :
        self . _mediator . entry_text_in_browser_request ( )
        original_name = self . _entry_text_in_browser
        filtered_name = ''
        for letter in original_name :
            if letter . isalnum ( ) :
                filtered_name += letter
            elif letter . isspace ( ) and not filtered_name [ - 1 : ] . isspace ( ) :
                filtered_name += letter
        filtered_name = filtered_name . strip ( )
        if original_name != filtered_name :
            self . _mediator . set_entry_text_in_browser ( filtered_name )
    def _if_entry_text_unique ( self ) :
        self . _mediator . entry_text_in_browser_request ( )
        return self . _entry_text_in_browser not in [ entry . name for entry in self . _entries ]
    def _proceed_to_browser_action ( self ) :
        self . _filter_entry_text ( )
        if not self . _only_unique_names or self . _if_entry_text_unique ( ) :
            self . _mediator . on_browser_action ( )
        else :
            self . _mediator . set_title_text_in_browser ( self . _already_exists_text )
    def _is_any_entries ( self ) :
        return len ( self . _all_knowledge_bases ) > 0
